<script setup>
import { onMounted, ref } from 'vue'
import useUserInfoStore from '@/stores/userInfo';
import { getAllClinicsService } from '@/api/hospital.js';
import { getAllDoctorsService, updateDoctorService } from '@/api/admin';
const userInfoStore = useUserInfoStore()

const userInfo = ref({ ...userInfoStore.info })
const doctorInfo = ref({})
const clinics = ref([])

const rules = {
    nickname: [
        { required: true, message: '请输入用户昵称', trigger: 'blur' },
        {
            pattern: /^\S{2,10}$/,
            message: '昵称必须是2-10位的非空字符串',
            trigger: 'blur'
        }
    ],
    email: [
        { required: true, message: '请输入用户邮箱', trigger: 'blur' },
        { type: 'email', message: '邮箱格式不正确', trigger: 'blur' }
    ],
    gender: [
        { required: true, message: '请选择性别', trigger: 'blur' }
    ],
    clinics: [
        { required: true, message: '请选择科室', trigger: 'blur' }
    ],
    phoneNumber:[
        { required: true, message: '请输入电话号码', trigger: 'blur' },
        { pattern: /^[1-9]\d{10}$/, message: '请输入正确的11位电话号码', trigger: 'blur' }
    ]
}
//获取所有医生信息，根据username匹配
const getAllDoctors = async () => {
    let result = await getAllDoctorsService();
    console.log(result.data);
    result.data.forEach(item => {
        if (item.username === userInfo.value.username) {
            doctorInfo.value = item
        }
    })
}
// 获取所有科室信息的函数
const fetchClinics = async () => {
    const response = await getAllClinicsService();
    clinics.value = response.data;
};
//修改用户信息
import { userInfoUpdateService } from '@/api/user.js'
import { ElMessage } from 'element-plus';
const updateUserInfo = async () => {
    await userInfoUpdateService(userInfo.value)
    await updateDoctorService(doctorInfo.value)
    ElMessage.success('修改成功')
    //更新pinia中的数据
    userInfoStore.setInfo(userInfo.value)
}
onMounted(() => {
    getAllDoctors();
    fetchClinics();
})
</script>
<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>基本资料</span>
            </div>
        </template>
        <el-row>
            <el-col :span="12">
                <el-form :model="doctorInfo" :rules="rules" label-width="100px" size="large">
                    <el-form-item label="登录名称">
                        <el-input v-model="userInfo.username" disabled></el-input>
                    </el-form-item>
                    <el-form-item label="身份信息">
                        <el-input v-model="userInfo.nickname" disabled></el-input>
                    </el-form-item>
                    <el-form-item label="医生姓名" prop="name">
                        <el-input v-model="doctorInfo.name"></el-input>
                    </el-form-item>
                    <el-form-item label="性别" prop="gender">
                        <el-select v-model="doctorInfo.gender" placeholder="请选择性别">
                            <el-option label="男" value="男"></el-option>
                            <el-option label="女" value="女"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="医生职位">
                        <el-input v-model="doctorInfo.title"></el-input>
                    </el-form-item>
                    <el-form-item label="用户邮箱" prop="email">
                        <el-input v-model="userInfo.email"></el-input>
                    </el-form-item>
                    <el-form-item label="电话号码" prop="phoneNumber">
                        <el-input v-model="doctorInfo.contactNumber"></el-input>
                    </el-form-item>
                    <el-form-item label="科室" prop="clinics">
                        <el-select v-model="doctorInfo.clinicId" placeholder="请选择科室">
                            <el-option v-for="clinic in clinics" :key="clinic.clinicId" :label="clinic.clinicName"
                                :value="clinic.clinicId" />
                        </el-select>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="updateUserInfo">提交修改</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </el-card>
</template>